package com.ydd.controller.admin;

import com.ydd.dto.GetSystemConfigDto;
import com.ydd.dto.LoginDTO;
import com.ydd.entity.SysUser;
import com.ydd.properties.JwtProperties;
import com.ydd.result.Result;
import com.ydd.service.indexSever;
import com.ydd.utils.JwtUtil;
import com.ydd.vo.BaseInfoVo;
import com.ydd.vo.GetConfigVO;
import com.ydd.vo.GetProfileVo;
import com.ydd.vo.TokenVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author Shadow
 * @Date 2023-04-28 11:37
 */
@RestController
@Api(tags = "工作台相关接口")
@RequestMapping("/index")
@Slf4j
public class UserControllor {
    @Autowired
    private  indexSever indexSever;
    @Autowired
    private JwtProperties jwtProperties; //涉及Bean


    //完成登录页面实现跳转主页
    @ApiOperation("登陆接口")
    @PostMapping("/login")
    public Result<TokenVo> login(@RequestBody LoginDTO loginDto){

        SysUser user=indexSever.ComparePassword(loginDto);

        Map<String, Object> claims = new HashMap<>();
        claims.put("userId",user.getId());


        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims
        );
        TokenVo tokenVo=new TokenVo();
        tokenVo.setToken(token);

        //返回token
        return Result.success(tokenVo);
    }

    @ApiOperation("获得系统配置接口")
    @GetMapping("/getConfig")
    private Result<GetConfigVO> getSystemConfig()
    {
        GetSystemConfigDto getSystemConfigDto=new GetSystemConfigDto();
        //DTO不能空值，因此一直爆红
        GetConfigVO systemConfig = indexSever.getSystemConfig(getSystemConfigDto);
        return Result.success(systemConfig);
    }

    @ApiOperation("主页统计信息接口")
    @GetMapping("/baseInfo")
    private Result<BaseInfoVo> getBaseinfo()
    {
        BaseInfoVo baseInfoVo=indexSever.getBaseinfo();
                return Result.success(baseInfoVo);
    }

    @ApiOperation("获取用户信息")
    @PostMapping("/profile")
    private Result<GetProfileVo> GetProfile()
    {
        GetProfileVo getProfileVo = indexSever.GetProfile();
        return Result.success(getProfileVo);
    }

    @GetMapping("/test")
    private Result test()
    {
        System.out.println("ok");
        return Result.success("ok");
    }
}
